package com.itheima.manager;

import com.itheima.common.R;
import com.itheima.domain.Menu;
import com.itheima.service.IMenuService;
import com.itheima.vo.MenuVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UmsMenuManager {
    @DubboReference
    IMenuService menuService;

    //树形结构返回所有菜单列表
    public R<List<MenuVo>> findTreeList() {
        List<MenuVo> list=menuService.findTreeList();
        return R.success(list);
    }

    //根据父级菜单id分页查询菜单
    public R<PageBeanVo> findByParentId(Long parentId, Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo=menuService.findByParentId(parentId,pageNum,pageSize);
        return R.success(pageBeanVo);
    }

    //添加菜单
    public R<String> saveMenu(Menu menu) {
        if(menu.getParentId()!=0){
            menu.setLevel(1);
        }else {
            menu.setLevel(0);
        }
        menuService.saveMenu(menu);
        return R.success("添加菜单成功");
    }
    //根据ID删除后台菜单
    public R<String> deleteMenu(Long id) {
        Boolean bool = menuService.deleteMenu(id);
        if(bool) {
            return R.success("删除菜单成功");
        }else{
            return R.error("此菜单下含有子菜单");
        }
    }

    //修改后台菜单
    public R<String> updateMenu(Menu menu) {
        menuService.updateMenu(menu);
        return R.success("修改菜单成功");
    }

    //修改菜单的显示状态
    public R<String> updateHidden(Long id, Integer hidden) {
        Menu menu = menuService.findById(id);
        menu.setHidden(hidden);
        menuService.updateMenu(menu);
        return R.success("修改菜单显示状态成功");
    }

    public R<Menu> findById(Long id) {
        return R.success(menuService.findById(id));
    }
}
